//
//  ViewController.m
//  Codex
//
//  Created by ChenJie on 2/8/21.
//

#import "ViewController.h"

@interface ViewController ()
<
UITableViewDataSource,
UITableViewDelegate
>

@property (nonatomic, strong) NSMutableArray *cells;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ViewController

#pragma mark - ViewController lifecycle

- (instancetype)init {
    self = [super init];
    if (self) {
        [self buildDataSource];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = NSStringFromClass(self.class);
    
    [self.view addSubview:self.tableView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    CGFloat topMark = 0;
    CGSize viewSize = self.view.bounds.size;
    
    self.tableView.frame = CGRectMake(0, topMark, viewSize.width, viewSize.height - topMark);
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

#pragma mark - Public methods



#pragma mark - Private methods

- (void)buildDataSource {
    NSArray *cells = @[
        @{@"title": @"One",
          @"target": @"UIViewController",
        },
        @{@"title": @"Two",
          @"action": @"cellActionTest",
        },
        @{@"class": UITableViewCell.class,
          @"title": @"Three",
          @"target": UIViewController.class,
        },
    ];
    
    [self.cells addObjectsFromArray:cells];
}

- (void)cellActionTest {
    NSLog(@"cell action test...");
}

#pragma mark - Event response methods



#pragma mark - Deletate methods



#pragma mark - <UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cells.count;
}

#pragma mark - <UITableViewDelegate>

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *cellInfo = self.cells[indexPath.row];
    
    Class cellClass = cellInfo[@"class"] ?: UITableViewCell.class;
    NSString *cellIdentifier = NSStringFromClass(cellClass);
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    cell.textLabel.text = cellInfo[@"title"];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *cellInfo = self.cells[indexPath.row];
    
    // push target
    if (cellInfo[@"target"]) {
        Class targetVcClass = cellInfo[@"target"] ?: UIViewController.class;
        if ([targetVcClass isKindOfClass:NSString.class]) {
            targetVcClass = NSClassFromString((NSString *)targetVcClass);
        }
        UIViewController *targetVc = [[targetVcClass alloc] init];
        if ([targetVc isMemberOfClass:UIViewController.class]) {
            targetVc.view.backgroundColor = [UIColor whiteColor];
            targetVc.navigationItem.title = NSStringFromClass(targetVc.class);
        }
        [self.navigationController pushViewController:targetVc animated:YES];
        return;
    }
    
    // action
    if (cellInfo[@"action"]) {
        NSString *selectorName = cellInfo[@"action"];
        SEL selector = NSSelectorFromString(selectorName);
        if (![self respondsToSelector:selector]) {
            return;
        }
        
        // invoke type 1
        /*
         IMP imp = [self methodForSelector:selector];
         void (*func)(id, SEL) = (void *)imp;
         func(self, selector);
         */
        
        // invoke type 2
        ((void (*)(id, SEL))[self methodForSelector:selector])(self, selector);
        return;
    }
    
}

#pragma mark - Setter



#pragma mark - Getter

- (NSMutableArray *)cells {
    if (!_cells) {
        NSMutableArray *array = [NSMutableArray array];
        _cells = array;
    }
    return _cells;
}

- (UITableView *)tableView {
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.dataSource = self;
        tableView.delegate = self;
//        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.tableFooterView = [UIView new];
        tableView.estimatedRowHeight = 0;
        
        _tableView = tableView;
    }
    return _tableView;
}

#pragma mark - Memory management

- (void)dealloc {
}

@end
